//
//  price_cell_company.m
//  car
//
//  Created by chenxu on 4/7/15.
//  Copyright (c) 2015 chenxu. All rights reserved.
//

#import "price_cell_company.h"

@interface price_cell_company()

@property (strong, nonatomic) UILabel *leftLabel;
@property (strong, nonatomic) UITextField *rightinput;

@end

@implementation price_cell_company

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.accessoryType = UITableViewCellAccessoryNone;
        if (!_leftLabel) {
            _leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 7, 300, 30)];
            _leftLabel.backgroundColor = [UIColor clearColor];
            _leftLabel.font = [UIFont systemFontOfSize:16];
            _leftLabel.textColor = [UIColor blackColor];
            [self.contentView addSubview:_leftLabel];
        }
        if (!_rightinput) {
            _rightinput = [[UITextField alloc]initWithFrame:CGRectMake(kScreen_Width - 160, 7, 150, 30)];
            [_rightinput setDelegate:self];
            [_rightinput setReturnKeyType:UIReturnKeyDone];
            _rightinput.textAlignment =  NSTextAlignmentRight;
            [self.contentView addSubview:_rightinput];
        }
        
    }
    return self;
}


- (void)setLeft:(NSString*)left right:(NSString*)right cellType:(com_pricetype)type{
    if (!left) {
        left = @"";
    }
    if (!right) {
        right = @"";
    }
    _leftLabel.text = left;
    switch (type) {
        case com_price_normal:
            _leftLabel.textColor = [UIColor blackColor];
            _rightinput.textColor = kColorTheme;
            if([right isKindOfClass:[NSNumber class]]){
                _rightinput.text = [NSString stringWithFormat:@"%ld",(long)[right integerValue]];
                _rightinput.text = [_rightinput.text stringByAppendingString:@"元"];
            }
            else {
                if ([right isEqualToString:@"0.0"]) {
                    _rightinput.placeholder = [right stringByAppendingString:@"元"];
                }
                else{
                    _rightinput.text = [right stringByAppendingString:@"元"];
                }
            }
            break;
            
        case com_price_total:
            _leftLabel.textColor = kColorTheme;
            _rightinput.textColor = kColorTheme;
            _rightinput.text = [right stringByAppendingString:@"元"];
            _rightinput.userInteractionEnabled = false;
    }
}



- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (_endEdit) {
        _endEdit(textField.text);
        [textField resignFirstResponder];
    }
    return YES;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (_onEdit) {
        _onEdit(textField.text);
    }
    return YES;
}

@end
